import {Session} from "mydog";
import {RoomMgr} from "../../../app/svr_game/roomMgr";
import {svr_game} from "../../../app/svr_game/svr_game";
import {getGameState} from "../../../app/util/gameUtil";
import {PlayerStatus} from "../../../app/svr_game/constants";


export default class Handler {
    private roomMgr: RoomMgr;
    constructor() {
        this.roomMgr = svr_game.roomMgr;
    }
    //切换场景接收协议
    enterTable(msg:any, session: Session, next: Function){
        let table = this.roomMgr.getTable(getGameState(session).tableId);
        if(table){
            table.enterTable(session.uid);
        }
    }
    // 离开
    leave(msg: any, session: Session, next: Function) {
        this.roomMgr.leave(getGameState(session).tableId,session.uid);
    }

    playStatus(msg: {status:PlayerStatus}, session: Session, next: Function) {
        let table = this.roomMgr.getTable(getGameState(session).tableId);
        if(!table){
            return;
        }
        if(msg.status==PlayerStatus.READY){
            table.ready(session.uid)
        }else if (msg.status==PlayerStatus.SITTING_UNREADY){
            table.unReady(session.uid)
        }
    }

    //游戏逻辑消息
    msg(msg:{event: string; data: any}, session: Session, next: Function){
        let table = this.roomMgr.getTable(getGameState(session).tableId);
        if(!table){
            return;
        }
        table.onGameMsg(session.uid,msg)
    }
}